
import { _decorator, Component, Node, Vec3, Game, math } from 'cc';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = CameraFollow
 * DateTime = Mon Mar 07 2022 12:05:09 GMT+0800 (中国标准时间)
 * Author = lamd
 * FileBasename = CameraFollow.ts
 * FileBasenameNoExtension = CameraFollow
 * URL = db://assets/Scripts/CameraFollow.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/en/
 *
 */

@ccclass('CameraFollow')
export class CameraFollow extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    lastPos: Vec3 = null!;
    // lastPlayerPos: Vec3 = null;
    delatZ: number = 0;

    init() {
        this.lastPos = this.node.worldPosition;
        this.delatZ = this.lastPos.z - GameMgr.instance.player.worldPosition.z
    }

    update(deltaTime: number) {
        if (GameMgr.instance.player == null) return;
        if (this.lastPos == null) return;

        let targetZ: number = GameMgr.instance.player.worldPosition.z + this.delatZ;
        let targetPos: Vec3 = new Vec3(this.lastPos.x, this.lastPos.y, targetZ);

        let outPos: Vec3 = new Vec3();
        Vec3.lerp(outPos, this.node.worldPosition, targetPos, 1 * deltaTime);
        this.node.worldPosition = outPos;

    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/en/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/en/scripting/life-cycle-callbacks.html
 */
